<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %> 
<%@ taglib uri="/WEB-INF/tld/sys.tld" prefix="sys" %>
<!DOCTYPE html>
<html lang="zh-CN" >
<head>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>垫款单明细列表</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>
<script type="text/javascript">
    var dfPayCount = 0; 
    var shPayCount = 0; 
    var dfPayAmt = 0; 
    var shPayAmt = 0;     
	var grid = null;
	var grid_pay = null;
	var ligerDialogPay = null;
	var handleDataArr = new Array();// 防止选中事件的多次调用造成的多长计算问题 
	$(function () {
        $("#tsxdht").bind("click",function(){
            pushContract();
        });
        $("#cstswj").bind("click",function(){
            pushQuestionnaire();
        });
        //更新测算结果
		$("#gxdkcsjg").bind("click",function(){
            updateCalcrstCd();
		});
		var timestamp=new Date().getTime(); 
		var _url = '<%=path%>/dk/dkbilldtl/initList.do?billId=${billId}'+'&timestamp = ' + timestamp;
	    grid = $("#ligergrid1").ligerGrid({
	    	url: _url,
	    	method : 'get',
	        columns: [
                { display: '支付表编号', align:'left', width: 80,
                    render: function (rowdata, rowindex, value){
                        return rowdata.paySchNo;
                    }
                },
                { display: '状态', width: 50,name: 'dtlStsCd',
                    render: function (rowdata, rowindex, value){
                       return rowdata.prdStsCd;
                    }
                },
                { display: '逾期天数', align:'left', width: 50,
                    render: function (rowdata, rowindex, value){
                        return rowdata.overdueDay;
                    }
                },
                { display: '逾期金额', align:'left', width: 100,
                    render: function (rowdata, rowindex, value){
                        return rowdata.overdueAmountYuan;
                    }
                },
                { display: '问卷结果', align:'left', width: 100,
                    render: function (rowdata, rowindex, value){
                        var qtnRstCd = rowdata.qtnRstCd;
                        if(qtnRstCd != null && qtnRstCd != ''){
                        	var res = "<a href=\"javascript:view('"+rowdata.ruleFlag+"')\">" + rowdata.qtnRstCd + "</a>"
                        	return res;
                    	}else{
                    		return "";
                    	}	
                    }
                },
                { display: '垫款测算结果', align:'left', width: 100,
                    render: function (rowdata, rowindex, value){
                        var calcRstCd = rowdata.calcRstCd;
                        return calcRstCd;
                    }
                },
                { display: '预计垫款/赎回金额', align:'left', width: 50,
                    render: function (rowdata, rowindex, value){
                        return rowdata.estimateRedeemAmtYuan;
                    }
                },
                { display: '垫款合同签署状态', align:'left', width: 100,
                    render: function (rowdata, rowindex, value){
                        var signStsCd =  rowdata.signStsCd;
                        return signStsCd;
                    }
                },
                { display: '推送时间',name:'pushTm', align:'left', width: 100,
                    render: function (rowdata, rowindex, value){
                        return renderDate2(rowdata, rowindex, value);
                    }
                },
                { display: '签署时间',name:'signTm', align:'left', width: 100,
                    render: function (rowdata, rowindex, value){
                        return renderDate2(rowdata, rowindex, value);
                    }
                },
                { display: '垫款状态', width: 50,name: 'dtlStsCd',
                    render: function (rowdata, rowindex, value){
                        var dtlStsCd = rowdata.dtlStsCd;
                        return dtlStsCd;
                    }
                },
                { display: '小贷账单编号', align:'left', width: 50,
                    render: function (rowdata, rowindex, value){
                        return rowdata.contNo;
                    }
                },
                { display: '实际垫款/赎回金额', align:'left', width: 150,
                    render: function (rowdata, rowindex, value){
                        return rowdata.actualRedeemAmtYuan;
                    }
                }
	        ],
	        pageSize:10,
	        root:'rows',
	        record:'total',
	        hideLoadButton : true,
	        width: '100%',
	        height:'100%',
	        heightDiff:-2,
	        //checkbox:true,
	        fixedCellHeight: false,
	        rownumbers:true,
			//onAfterShowData: getFlowIds,
	    });
	    grid = liger.get("ligergrid1");
	    grid.setHeight(100);
	    $("#searchBar").ligerForm();
	
	    $("#searchBtn").click(function(event) {
	         var form = new liger.get("searchBar");
	         var data = form.getData();
	         console.log(JSON.stringify(data));
	         for (name in data){
	            grid.setParm(name,data[name]);
	         };
	        grid.reload();
	    });
	    
	    $("#clearBtn").click(function(event) {
	        $("#resetBtn").click();  //重置 input-text
	        var form = liger.get("searchBar");
	        form.setData({  //重置select等特殊表单
	            "qtnRstCd": " ",
	            "calcRstCd": " ",
                "signStsCd": " ",
                "dtlStsCd": " "
	        });
	    });
	    
	    $("#zxdk").bind("click",function(){
	    		// 打开前清空数组
	    		handleDataArr.splice(0,handleDataArr.length);//清空数组 
	    		// 置null 
	    		dfPayCount = 0;
	    		shPayCount = 0;
	    		dfPayAmt = 0;
	    		shPayAmt = 0;
	    		$("#dfPayCount").text(dfPayCount);
		        $("#shPayCount").text(shPayCount);
		        $("#dfPayAmt").text(dfPayAmt);
		        $("#shPayAmt").text(shPayAmt);
		        
		    	ligerDialogPay =  $.ligerDialog.open({
		            width: 900,
		            height:500,
		            top: 5,
		            title : '执行垫付',
		            target: $("#dialogPay_Html"),
		            onHideDialog: function () {
		              clearLigerForm("searchBar_Pay");
		            } 
		          }); 
		          
		          initGridPay();
		         /**
		          *计算合计金额
		          *initHandleAmts();
		          */
			});
			
		$("#cancelBtn_pay").bind("click",function(){ 
			ligerDialogPay.hidden();
	    });
	    
	    $("#saveBtn_pay").bind("click",function(){
	    	var selectedRows = grid_pay.getSelectedRows();
			if(selectedRows.length == 0){
				$.ligerDialog.SQalert({content: '请选择后在执行!', SQ_type: "warring"});
				return;
			}
			
			var ids = new Array();
			for (var i=0; i < selectedRows.length; i++) {
                ids.push(selectedRows[i].id);
			}
            var url = '<%=path%>/dk/dkbilldtl/doDk.do';
            $.ajax({
                type: 'get',
                dataType:'json',
                data: {ids: JSON.stringify(ids)},
                url: url,
                success:function(msg){
                    if(msg.code == '1'){
                        $.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
                        grid.reload();
                        ligerDialogPay.hidden();
                    }else{
                        $.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
                    }
                }
            });
	    });
		    
		$("#dcshsj").bind("click",function(){ 
			var url = '<%=path%>/dk/dkbilldtl/exportBillDtlExcel.do';
			$('#searchBar').attr('action', url);  
	        $('#searchBar').submit();
	        
	        <%-- 
            var form = new liger.get("searchBar");
	        var data = form.getData();
			var wait_dialog = null;
			$.ajax({
				type: 'get',
				dataType:'json',
				url: '<%=path%>/dk/dkbilldtl/exportBillDtlExcel.do',
				data:data,
				beforeSend:function(){
					wait_dialog=$.ligerDialog.SQalert({content: '正在导出停车场信息...',SQ_type: "waitting"});
				},
				success:function(msg){
					wait_dialog.close();
					if(msg.success == 1){
						window.open(msg.data);
					}else{
						$.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
					}
				} 
			}); --%>
	    });    
		    
	});

    /**
     *  推送小贷合同
     */
    function pushContract(){
        var url = '<%=path%>/dk/dkbilldtl/pushContract.do';
        var billId = '${billId}';
        $.ajax({
            type: 'get',
            dataType:'json',
            data: {billId:billId},
            url: url,
            success:function(msg){
                if(msg.success == '1'){
                    $.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
                    grid.reload();
                }else{
                    $.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
                }
            }
        });
    }
	function pushQuestionnaire(){
			var url = '<%=path%>/dk/dkbilldtl/pushQuestionnaire.do';
	        var billId = '${billId}';
	        $.ajax({
			type: 'get',
			dataType:'json',
			data: {billId:billId},
			url: url,
			success:function(msg){
				if(msg.success == '1'){
            		$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
            		grid.reload();
            	}else{
            		$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
            	}
			}
		});
		
	}
    //格式化时间
    function renderDate2(rowdata, index, value) {
        var dateStr = "";
        if (null != value) {
            var date = new Date(value);
            //dateStr = DateUtils.formatDate(date, "yyyy-MM-dd hh:mm");
            dateStr = DateUtils.formatDate(date, "yyyy-MM-dd");
        }
        return dateStr;
    }
    //更新垫款测算结果
    function updateCalcrstCd(){
        var url = '<%=path%>/dk/dkbilldtl/updateCalcrstCd.do';
        var billId = '${billId}';
        $.ajax({
            type: 'get',
            dataType:'json',
            data: {billId:billId},
            url: url,
            success:function(msg){
                if(msg.code == '1'){
                    $.ligerDialog.SQalert({content: '处理成功!',SQ_type: "success"});
                    grid.reload();
                }else{
                    $.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
                }
            }
        });

    }
    
    //初始化列表
	function initGridPay(){
		var params = {};
		var timestamp=new Date().getTime(); 
	   	grid_pay = $("#grid_pay").ligerGrid({
	        url: '<%=path%>/dk/dkbilldtl/searchList.do?billId=${billId}'+'&timestamp = ' + timestamp,
	        method : 'get',
	        columns: [
				{ display: '支付表编号', name: 'paySchNo', minWidth: 100,align: 'left', enabledSort:true },
				{ display: '状态', name: 'prdStsCdNm', minWidth: 30, enabledSort:true},
				{ display: '逾期天数', name: 'overdueDay', minWidth: 30 , enabledSort:true},
				{ display: '逾期金额', name: 'overdueAmountYuan', minWidth: 50 , enabledSort:true},
				{ display: '预计垫款/赎回金额', name: 'estimateRedeemAmtYuan', minWidth: 30 , enabledSort:true},
				{ display: '垫款测算结果', name: 'calcRstCd', minWidth: 120 ,align:'left',
  	            		render: function (rowdata, rowindex, value, column) { 
  	            				if(value == '1'){
  	            					return '垫款';
  	            				}else if(value == '2'){
  	            					return '赎回';
  	            				}
                     		 	return '';
  	            			},
  	            	enabledSort:true},
           		{ display: '垫款合同签署状态', name: 'signStsCd', minWidth: 30 ,align:'left',
	            		render: function (rowdata, rowindex, value, column) { 
	            				if(value == '0'){
	            					return '未签署';
	            				}else if(value == '1'){
	            					return '签署成功';
	            				}else if(value == '2'){
	            					return '签署失败';
	            				}
                   		 		return '';
	            			},
	            	enabledSort:true},
            	{display: '推送时间',name:'pushTm', minWidth: 30 ,align:'left',
            		render: function (rowdata, rowindex, value, column) { 
            				return renderDate2(rowdata, rowindex, value);
            			},
            		enabledSort:true},
            	{ display: '签署时间',name:'signTm', minWidth: 30 ,align:'left',
            		render: function (rowdata, rowindex, value, column) { 
            				return renderDate2(rowdata, rowindex, value);
            			},
           		enabledSort:true}
	        ],
	        pageSize:10,
	        root:'rows',
	        record:'total',
	        usePager:false,
	        hideLoadButton : true,
	        width: '100%',
	        height: 400,
	        heightDiff: -2,
	        checkbox:true,
	        allowUnSelectRow:true,
	        alternatingRow: false,
	        onCheckRow:function(checked,data,rowid,rowobj){
	        	handleData(checked,data);
	        }
    	});
		grid_pay.setHeight(500);
	   	$("#searchBar_pay").ligerForm();
	}
    function view(ruleFlag){
        parent.Add_navTab('dk_qtn_view'+ruleFlag,'查看问卷','/dk/dkquestnelog/view.do?ruleFlag='+ruleFlag);
    }
	function handleData(checked,data){
		if(checked){
			// 数组中有该值了，不用再次执行了，不然会导致计算错误 
			if(handleDataArr.indexOf(data.id) != -1 ){
				return;
			}
			// 数组添加元素 
       		handleDataArr.push(data.id);
       		if(data.calcRstCd == '1'){// 垫付
       			dfPayCount = Number(dfPayCount) + 1;
       			dfPayAmt = Number(dfPayAmt) + Number(data.estimateRedeemAmtYuan);
       			$("#dfPayCount").text(dfPayCount);
       			$("#dfPayAmt").text(dfPayAmt);
       		}else if(data.calcRstCd == '2'){// 赎回
       			shPayCount = Number(shPayCount) + 1;
       			shPayAmt = Number(shPayAmt) + Number(data.estimateRedeemAmtYuan);
       			$("#shPayCount").text(shPayCount);
	        	$("#shPayAmt").text(shPayAmt);
       		}
       	}else{
       		// 数组删除元素 
       		handleDataArr.splice($.inArray(data.id, handleDataArr), 1); 
       		if(data.calcRstCd == '1'){// 垫付
       			dfPayCount = Number(dfPayCount) - 1;
       			dfPayAmt = Number(dfPayAmt) - Number(data.estimateRedeemAmtYuan);
       			if(dfPayCount < 0){
       				dfPayCount = 0;
       			}
       			if(dfPayAmt < 0){
       				dfPayAmt = 0;
       			}
       			$("#dfPayCount").text(dfPayCount);
       			$("#dfPayAmt").text(dfPayAmt);
       		}else if(data.calcRstCd == '2'){// 赎回
       			shPayCount = Number(shPayCount) - 1;
       			shPayAmt = Number(shPayAmt) - Number(data.estimateRedeemAmtYuan);
       			if(shPayCount < 0){
       				shPayCount = 0;
       			}
       			if(shPayAmt < 0){
       				shPayAmt = 0;
       			}
       			$("#shPayCount").text(shPayCount);
	        	$("#shPayAmt").text(shPayAmt);
       		}
       	}
	}
</script>

<body class="iframeBody">
<div class="Wrapper">
<div id="pageInTab" class="pageInTab">
<div tabid="tabA" title="垫款单明细列表" lselected="true">
<div class="searchArea">
    <form id="searchBar">
        <input name="billId" id="billId" type="hidden" value="${billId}" class="ui-textbox">
        <div class="fieldset1 clearFix">
            <div class="fieldBox">
                <div class="label">问卷结果</div>
                <div class="field">
                    <select name="qtnRstCd" id="qtnRstCd" data-value=" ">
                        <option value=" " seleted>请选择</option>
                        <c:forEach items="${stsCdList}" var="kv">
                            <c:if test="${kv.key == 'DK0010'}">
                                <c:forEach items="${kv.value}" var="kv2">
                                    <option value="${kv2.key}">${kv2.value}</option>
                                </c:forEach>
                            </c:if>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="fieldBox">
                <div class="label">垫款测算结果</div>
                <div class="field">
                    <select name="calcRstCd" id="calcRstCd" data-value=" ">
                        <option value=" " seleted>请选择</option>
                        <c:forEach items="${stsCdList}" var="kv">
                            <c:if test="${kv.key == 'DK0004'}">
                                <c:forEach items="${kv.value}" var="kv2">
                                    <option value="${kv2.key}">${kv2.value}</option>
                                </c:forEach>
                            </c:if>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="fieldBox">
                <div class="label">合同签署状态</div>
                <div class="field">
                    <select name="signStsCd" id="signStsCd" data-value=" ">
                        <option value=" " seleted>请选择</option>
                        <c:forEach items="${stsCdList}" var="kv">
                            <c:if test="${kv.key == 'DK0007'}">
                                <c:forEach items="${kv.value}" var="kv2">
                                    <option value="${kv2.key}">${kv2.value}</option>
                                </c:forEach>
                            </c:if>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="fieldBox">
                <div class="label">垫款状态</div>
                <div class="field">
                    <select name="dtlStsCd" id="dtlStsCd" data-value=" ">
                        <option value=" " seleted>请选择</option>
                        <c:forEach items="${stsCdList}" var="kv">
                            <c:if test="${kv.key == 'DK0005'}">
                                <c:forEach items="${kv.value}" var="kv2">
                                    <option value="${kv2.key }">${kv2.value }</option>
                                </c:forEach>
                            </c:if>
                        </c:forEach>
                    </select>
                </div>
            </div>


            <div class="clearFix"></div>
			<div class="btnsBox formBtns notRight">
                <span id="searchBtn" class="sq-button-round">查询</span>
                <span id="clearBtn" class="sq-button-round">清空</span>
                <input id="resetBtn" type="reset" value="重置" class="l-button l-button-reset Hide"/>
                <span id="cstswj" class="sq-button-round">测算推送问卷</span>
                <span id="gxdkcsjg" class="sq-button-round">更新垫款测算结果</span>
                <span id="tsxdht" class="sq-button-round">推送小贷合同</span>
                <span id="zxdk" class="sq-button-round">执行垫款</span>
                <span id="dcshsj" class="sq-button-round">导出赎回数据</span>
            </div>
		</div>
	</form>
    </div>
	<div id="ligergrid1" class="sq-grid"></div>
	
	<!-- 执行垫付 start  -->
	<div id="dialogPay_Html" class="searchArea" style="display:none">
	    <form id="searchBar_Pay">
	    </form>  
	    <div>
    		<div id="grid_pay" class="sq-grid"></div>
    		<div class="fieldset1 tabel_col clearFix">
              	<div class="fieldBox col_20">
                  	<div class="label">垫付合计：</div>
              	</div>
              	<div class="fieldBox col_20">
                  	<div class="label">支付表：</div>
                  	<div class="field" id="dfPayCount"> </div>
              	</div>
              	<div class="fieldBox col_60">
                  	<div class="label">金额：</div>
                  	<div class="field" id="dfPayAmt"> </div>
              	</div>
    		</div>
    		
    		<div class="fieldset1 tabel_col clearFix">
              	<div class="fieldBox col_20">
                  	<div class="label">赎回合计：</div>
              	</div>
              	<div class="fieldBox col_20">
                  	<div class="label">支付表：</div>
                  	<div class="field" id="shPayCount"> </div>
              	</div>
              	<div class="fieldBox col_60">
                  	<div class="label">金额：</div>
                  	<div class="field" id="shPayAmt"> </div>
              	</div>
    		</div>
    		
    		<div class="btnsBox">
   				<span id="cancelBtn_pay" class="sq-button sq-button-round">取消</span>
                <span id="saveBtn_pay" class="sq-button sq-button-round">执行</span>
            </div>
    	</div>
	</div>
	<!-- 执行垫付 end  -->
	
</div>
</div>
</div>
</body>
</html>